//------------------------------------------------------------------------------------------------
// The MIT License (MIT)
// 
// Copyright (c) 2019 Crsky
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//------------------------------------------------------------------------------------------------

#ifndef FILE_SYSTEM_H
#define FILE_SYSTEM_H

//------------------------------------------------------------------------------------------------
// Declares
//------------------------------------------------------------------------------------------------

using FileHandle_t = intptr_t;

constexpr FileHandle_t INVALID_FILE_HANDLE = static_cast<FileHandle_t>( 0 );

//------------------------------------------------------------------------------------------------
// Interface to file I/O
//------------------------------------------------------------------------------------------------
class IFileSystem
{
public:
	virtual bool Init( const wstring& root_dir ) = 0;
	virtual void Shutdown() = 0;

	// For the seek function
	enum class SeekType { Set, Cur, End };

	// Open a file
	virtual FileHandle_t	Open( const wstring& file_name, const char* mode ) = 0;

	// Close file
	virtual void			Close( FileHandle_t handle ) = 0;

	// Reads data from a file
	virtual int64_t			Read( FileHandle_t handle, void* buffer, int64_t buffer_size ) = 0;

	// Writes data to a file
	virtual int64_t			Write( FileHandle_t handle, const void* buffer, int64_t buffer_size ) = 0;

	// Moves the current position to a specified location
	virtual bool			Seek( FileHandle_t handle, SeekType seek_type, int64_t offset ) = 0;

	// Gets the current position of a file handle
	virtual int64_t			Tell( FileHandle_t handle ) = 0;
};

//------------------------------------------------------------------------------------------------
// Returns the singleton of the file system
//------------------------------------------------------------------------------------------------
IFileSystem * FileSystem();

#endif
